﻿Imports Global_Visas_Business_Layer
Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Controls.Primitives
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports System.Xml
Imports System.IO
Imports System.Data
Partial Public Class frmAdminStockLetterSets
    Private Frm_ViewStage As Integer
    Private Frm_ClientType As Integer
    Private Sub frmAdminStockLetterSets_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Call SQL_SLSType()

    End Sub

    Public Sub SQL_SLSType()

        Dim StrSQL As String

        StrSQL = "Select SLSType_ID, SLSType_Name from I_SL_SLSTypes Order By SLSType_Name"
        Dim slsType As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
        With Combo_SLSType
            .ItemsSource = CType(slsType, IListSource).GetList
            .DisplayMemberPath = "SLSType_Name"
            .SelectedValuePath = "SLSType_ID"
            .SelectedIndex = 0
        End With

        Call SQL_SLSList()

    End Sub

    Public Sub SQL_SLSList()

        Dim StrSQL As String
        StrSQL = "Select SLS_ID, SLS_Name from I_SL_StockLetterSets"
        If (Me.Combo_SLSType.SelectedIndex <> -1) Then
            StrSQL = StrSQL & " Where SLS_TypeID = " & Me.Combo_SLSType.SelectedValue
        End If
        StrSQL = StrSQL & " Order By SLS_Name"
        Dim slsList As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
        DataBinding.BindListView(slsList, List_SLS)


    End Sub

    Private Sub Btn_Close_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Close.Click
        Me.Close()
    End Sub

    Private Sub Combo_SLSType_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles Combo_SLSType.SelectionChanged
        SQL_SLSList()
    End Sub

    Private Sub List_SLS_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles List_SLS.SelectionChanged
        List_SLS_AfterUpdate()
    End Sub
    Private Sub List_SLS_AfterUpdate()
        Dim StrSQL As String
        Dim selectedSls As DataRowView = Me.List_SLS.SelectedItem
        If selectedSls Is Nothing Then Exit Sub

        Call SQL_LetterPackList()

        Me.List_LetterPacks.IsEnabled = True
        Me.GroupBox1.IsEnabled = True
        Me.GroupBox2.IsEnabled = True

        StrSQL = "SELECT Product_ID, Product_Name"
        StrSQL = StrSQL & " FROM L_Products"
        StrSQL = StrSQL & " WHERE Product_SLSID = " & selectedSls(0)

        Dim productList As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
        DataBinding.BindListView(productList, List_Products)


        Me.List_Products.IsEnabled = True

        'StrSQL = "SELECT Product_ID, Product_Name"
        'StrSQL = StrSQL & " FROM L_Products Order By Product_Name Asc"

        'Dim productsSLS As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
        'With Me.Combo_CopyFrom
        '    .Items.Clear()
        '    .ItemsSource = CType(productsSLS, IListSource).GetList
        '    .SelectedValuePath = "Product_ID"
        '    .DisplayMemberPath = "Product_Name"
        '    .SelectedIndex = -1
        'End With

        StrSQL = "SELECT ProductType_ID, ProductType_Name"
        StrSQL = StrSQL & " FROM L_ProductTypes"
        StrSQL = StrSQL & " WHERE ProductType_SLSID = " & selectedSls(0)

        Dim productTypes As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
        DataBinding.BindListView(productTypes, List_ProductTypes)

        Me.List_ProductTypes.IsEnabled = True

        StrSQL = "Select SLS_ID, SLS_Name, SLS_TypeID "
        StrSQL = StrSQL & " From I_SL_StockLetterSets"
        StrSQL = StrSQL & " Inner Join I_SL_SLSLink on I_SL_StockLetterSets.SLS_ID = I_SL_SLSLink.SLSLink_SLSID"
        StrSQL = StrSQL & " Where I_SL_SLSLink.SLSLink_ChildSLSID = " & selectedSls(0)
        StrSQL = StrSQL & " Order By SLS_ID"

        Dim listUsedinSls As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
        DataBinding.BindListView(listUsedinSls, List_UsedInSLS)

        Me.List_UsedInSLS.IsEnabled = True

        Me.Btn_AddLetterPack.IsEnabled = True
        Me.Btn_DeleteSLS.IsEnabled = True
        Me.Btn_CopyFrom.IsEnabled = True
        Me.Btn_AddChildSLS.IsEnabled = True
        Me.Combo_CopyFrom.IsEnabled = True
        Me.Btn_EMailTemplates.IsEnabled = True

        Me.Btn_Delete.IsEnabled = False
        Me.Btn_EditLink.IsEnabled = False
        Me.Btn_MoveDown.IsEnabled = False
        'Me.Btn_MoveUp.IsEnabled = False
        Me.Btn_MoveTop.IsEnabled = False
        Me.Btn_MoveBottom.IsEnabled = False

    End Sub
    Public Sub SQL_LetterPackList()

        Dim StrSQL As String
        Dim selectedSLS As DataRowView = Me.List_SLS.SelectedItem
        If selectedSLS Is Nothing Then Exit Sub
        'StrSQL = Gblfunc_Get_SLSSQL(Me.List_SLS, Me.Frm_ClientType, Me.Frm_ViewStage, 2)

        StrSQL = "SELECT I_SL_SLSLink.SLSLink_ID, D_LetterPacks.LetterPack_ID as ID, D_LetterPacks.LetterPack_Name as LetterPack,"
        StrSQL = StrSQL & " CASE I_SL_SLSLink.SLSLink_CustomerType WHEN 1 THEN 'Private' WHEN 2 THEN 'Corporate' ELSE 'All' END AS Clients,"
        StrSQL = StrSQL & " Case I_SL_SLSLink.SLSLink_StageStart when 0 then 'New Lead' when 100 then 'Initiated' else L_CaseStages_Start.CaseStage_Title end AS  Start, "
        StrSQL = StrSQL & " case I_SL_SLSLink.SLSLink_StageEnd when 99 then 'Sold' when 999 then 'Closed' when 0 then 'After Closed' else L_CaseStages_End.CaseStage_Title end AS  'End', I_SL_SLSLink.SLSLink_Order as 'Order'"

        StrSQL = StrSQL & " FROM I_SL_SLSLink INNER JOIN"
        StrSQL = StrSQL & " D_LetterPacks ON I_SL_SLSLink.SLSLink_LetterPackID = D_LetterPacks.LetterPack_ID LEFT OUTER JOIN"
        StrSQL = StrSQL & " L_CaseStages L_CaseStages_End ON I_SL_SLSLink.SLSLink_StageEnd = L_CaseStages_End.CaseStage_Number LEFT OUTER JOIN"
        StrSQL = StrSQL & " L_CaseStages L_CaseStages_Start ON I_SL_SLSLink.SLSLink_StageStart = L_CaseStages_Start.CaseStage_Number"

        StrSQL = StrSQL & " WHERE I_SL_SLSLink.SLSLink_SLSID = " & selectedSLS(0)

        Select Case Me.Frm_ViewStage

            Case 1
                'All, no addition

            Case 2
                'Pre-Sale only
                StrSQL = StrSQL & " AND I_SL_SLSLink.SLSLink_StageEnd < 100"

            Case 3
                'Initiated only
                StrSQL = StrSQL & " AND I_SL_SLSLink.SLSLink_StageStart > 99"

        End Select

        Select Case Me.Frm_ClientType

            Case 0
                'All, no addition

            Case 1
                'Private clients

                StrSQL = StrSQL & " AND I_SL_SLSLink.SLSLink_CustomerType in (0,1)"

            Case 2
                'Corporate clients

                StrSQL = StrSQL & " AND I_SL_SLSLink.SLSLink_CustomerType in (0,2)"

        End Select

        StrSQL = StrSQL & " ORDER BY I_SL_SLSLink.SLSLink_Order"


        Dim letterPacks As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)

        Me.List_LetterPacks.ItemsSource = letterPacks
        Me.List_LetterPacks.Columns(0).IsVisible = False

    End Sub



    Private Sub Option12_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option12.Checked
        Me.Frm_ViewStage = 1
        SQL_LetterPackList()
    End Sub

    Private Sub Option14_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option14.Checked
        Me.Frm_ViewStage = 2
        SQL_LetterPackList()
    End Sub

    Private Sub Option16_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option16.Checked
        Me.Frm_ViewStage = 3
        SQL_LetterPackList()
    End Sub

    Private Sub Option56_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option56.Checked
        Me.Frm_ViewStage = 4
        SQL_LetterPackList()
    End Sub

    Private Sub Option21_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option21.Checked
        Me.Frm_ClientType = 0
        SQL_LetterPackList()
    End Sub

    Private Sub Option23_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option23.Checked
        Me.Frm_ClientType = 1
        SQL_LetterPackList()
    End Sub

    Private Sub Option25_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option25.Checked
        Me.Frm_ClientType = 2
        SQL_LetterPackList()
    End Sub

    Private Sub Btn_NewSLS_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_NewSLS.Click

        Try
            Dim StrSQL As String, StrMess As String, IntSLS As Integer, StrSLS As String, Rst As DataTable

            StrSLS = InputBox("Please enter a name for the new Stock Letter Set", "Create SLS")
            If StrSLS.Trim = "" Then
                Call MsgBox("Action cancelled")
                Exit Sub
            End If

            StrSQL = "Select SLS_ID, SLS_TypeID from I_SL_StockLetterSets where SLS_Name = '" & StrSLS.Trim & "'"
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then
                If Rst.Rows(0)("SLS_TypeID") <> Me.Combo_SLSType.SelectedValue Then
                    Me.Combo_SLSType = Rst.Rows(0)("SLS_TypeID")
                    Call SQL_SLSList()
                End If
                Me.List_SLS = Rst.Rows(0)("SLS_ID")
                Call List_SLS_AfterUpdate()
                Rst.Dispose()
                Call MsgBox("An SLS with that name already exists.", , "Error")
                Exit Sub
            End If
            Rst.Dispose()

            If Len(StrSLS) > 50 Then
                Call MsgBox("Name cannot be more than 50 characters", , "Error")
                Exit Sub
            End If


            If (Me.Combo_SLSType.SelectedIndex <> -1) Then
                StrSQL = "Insert into I_SL_StockLetterSets (SLS_Name, SLS_TypeID) VALUES('" & StrSLS.Trim & "'"
                StrSQL = StrSQL & ", " & Me.Combo_SLSType.SelectedValue
            Else
                StrSQL = "Insert into I_SL_StockLetterSets (SLS_Name) VALUES('" & StrSLS.Trim & "'"
            End If
            StrSQL = StrSQL & ")"
            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            StrSQL = "Select SLS_ID from I_SL_StockLetterSets where SLS_Name = '" & StrSLS.ToString & "'"
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                'Error
                Rst.Dispose()
                Call MsgBox("New SLS not found", , "Error")
                Exit Sub
            End If

            IntSLS = Rst.Rows(0)("SLS_ID")
            Rst.Dispose()

            StrMess = "Stock Letter Set No. " & IntSLS & " (" & StrSLS & ") of type " & Me.Combo_SLSType.Text & " created"

            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrMess)

            Me.SQL_SLSList()
            'Me.List_SLS = IntSLS
            Call List_SLS_AfterUpdate()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminStockLetterSets-Btn_NewSLS_Click")
        End Try
    End Sub

    Private Sub Btn_AddLetterPack_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_AddLetterPack.Click
        Try
            Dim StrSQL As String
            Dim selectedList_SLSItem As DataRowView = Me.List_SLS.SelectedItem
            If selectedList_SLSItem Is Nothing Then
                MsgBox("Select the Stock Letter Set first.", , "Error")
                Exit Sub
            End If

            Dim formAddLetterPackToSLS As New frmAdminAddLetterpackToSLS
            StrSQL = "SELECT LetterType_ID, LetterType_Name"
            StrSQL = StrSQL & " FROM I_SL_LetterTypes"
            StrSQL = StrSQL & " WHERE (LetterType_DataGroupID = 2)"
            StrSQL = StrSQL & " Order By LetterType_Name"

            Dim letterPacks As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
            With formAddLetterPackToSLS.ComboLetterTypes
                .ItemsSource = CType(letterPacks, IListSource).GetList
                .SelectedValuePath = "LetterType_ID"
                .DisplayMemberPath = "LetterType_Name"
                .SelectedIndex = -1
            End With
            formAddLetterPackToSLS.List_SLS = selectedList_SLSItem(0)
            'formAddLetterPackToSLS.SQL_LetterPackList()
            formAddLetterPackToSLS.ShowDialog()
            Me.SQL_LetterPackList()
            List_Letterpacks_AfterUpdate()


            'Call GblFunc_Populate_List(StrSQL, "SLS_AddLetterPack_Types", Form_FMA_AddLetterPackToSLS.Combo_LetterTypes)

            'Call Form_FMA_AddLetterPackToSLS.SQL_LetterPackList()

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminStockLetterSets-Btn_AddLetterPack_Click")
        End Try
    End Sub

    Private Sub Btn_DeleteSLS_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_DeleteSLS.Click
        Try
            Dim StrSQL As String, StrMess As String, Rst As DataTable
            Dim selectedSLS As DataRowView = Me.List_SLS.SelectedItem
            If selectedSLS Is Nothing Then Exit Sub

            If selectedSLS(0) = 34 Then
                Call MsgBox("The 'No Product' SLS is a protected system set and cannot be deleted", , "Error")
                Exit Sub
            End If

            StrSQL = "Select Product_Name from L_Products Where Product_SLSID = " & selectedSLS(0)
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then

                For Each currentRow As DataRow In Rst.Rows
                    StrMess = StrMess & currentRow("Product_Name") & vbCrLf
                Next

                'Do
                '    StrMess = StrMess & Rst.Fields("Product_Name") & vbCrLf
                '    Rst.MoveNext()
                'Loop While Not Rst.EOF


                StrMess = "Cannot delete, this SLS is use with the following products:" & vbCrLf & vbCrLf & StrMess
                StrMess = StrMess & "Please assign another SLS to these products then try again."
                Call MsgBox(StrMess, , "Error")
                Exit Sub
            End If

            Rst.Dispose()

            StrSQL = "Select ProductType_Name from L_ProductTypes Where ProductType_SLSID = " & selectedSLS(0)
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then
                'Rst.MoveFirst()
                For Each currentRow As DataRow In Rst.Rows
                    StrMess = StrMess & currentRow("ProductType_Name") & vbCrLf
                Next

                'Do
                '    StrMess = StrMess & Rst.Fields("ProductType_Name") & vbCrLf
                '    Rst.MoveNext()
                'Loop While Not Rst.EOF
                'Rst.Close()

                StrMess = "Cannot delete, this SLS is use with the following product types:" & vbCrLf & vbCrLf & StrMess
                StrMess = StrMess & "Please assign another SLS to these product types then try again."
                Call MsgBox(StrMess, , "Error")
                Exit Sub
            End If

            Rst.Dispose()

            StrSQL = "Select SLS_ID, SLS_Name "
            StrSQL = StrSQL & " From I_SL_StockLetterSets"
            StrSQL = StrSQL & " Inner Join I_SL_SLSLink on I_SL_StockLetterSets.SLS_ID = I_SL_SLSLink.SLSLink_SLSID"
            StrSQL = StrSQL & " Where I_SL_SLSLink.SLSLInk_ChildSLSID = " & selectedSLS(0)

            'MJG 04/08/06 This line of code was missing which meant previous SQL was never executed
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then
                'Rst.MoveFirst()
                For Each currentRow As DataRow In Rst.Rows
                    StrMess = StrMess & currentRow("SLS_Name") & vbCrLf
                Next

                'Do
                '    StrMess = StrMess & Rst.Fields("SLS_Name") & vbCrLf
                '    Rst.MoveNext()
                'Loop While Not Rst.EOF
                Rst.Dispose()

                StrMess = "Cannot delete, this SLS is use in the following Stock Letter Sets:" & vbCrLf & vbCrLf & StrMess
                StrMess = StrMess & "Please remove from those sets then try again."
                Call MsgBox(StrMess, , "Error")
                Exit Sub
            End If

            Rst.Dispose()

            If MsgBox("Are you sure you want to delete this Stock Letter Set?", vbYesNo, "Confirm Delete") <> 6 Then Exit Sub

            StrMess = "Stock Letter Set no. " & selectedSLS(0) & " (" & selectedSLS(1) & ") deleted"

            StrSQL = "Delete from I_SL_SLSLink where SLSLink_SLSID = " & selectedSLS(0)
            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            StrSQL = "Delete from I_SL_StockLetterSets where SLS_ID = " & selectedSLS(0)
            If CommonDataFunctions.ExecuteSQL(StrSQL) = 0 Then Exit Sub

            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrSQL)

            'Initialise form
            Me.SQL_SLSList()
            'Me.List_SLS.Requery()
            Me.List_LetterPacks.ItemsSource = Nothing
            SQL_LetterPackList()
            Me.List_LetterPacks.IsEnabled = False
            Me.List_Products.ItemsSource = Nothing
            'Me.List_Products.Requery()
            'Me.List_Products.Enabled = False
            'Me.List_ProductTypes.RowSource = ""
            ' Me.List_ProductTypes.Requery()
            'Me.List_ProductTypes.Enabled = False

            'Me.List_SLS.SetFocus()

            Me.Btn_AddLetterPack.IsEnabled = False
            Me.Btn_Delete.IsEnabled = False
            Me.Btn_DeleteSLS.IsEnabled = False
            Me.Btn_EditLink.IsEnabled = False
            'Me.Btn_MoveDown.Enabled = False
            'Me.Btn_MoveUp.Enabled = False
            'Me.Btn_MoveTop.Enabled = False
            'Me.Btn_MoveBottom.Enabled = False
            Me.Btn_CopyFrom.IsEnabled = False
            Me.Combo_CopyFrom.IsEnabled = False
            Me.Btn_EMailTemplates.IsEnabled = False

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminStockLetterSets-Btn_DeleteSLS_Click")
        End Try
        
    End Sub

    Private Sub List_LetterPacks_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles List_LetterPacks.SelectionChanged
        List_Letterpacks_AfterUpdate()
    End Sub
    Private Sub List_Letterpacks_AfterUpdate()
        Me.Btn_Delete.IsEnabled = True
        Me.Btn_EditLink.IsEnabled = True
        'Me.Btn_MoveDown.IsEnabled = True
        'Me.Btn_MoveUp.IsEnabled = True
        'Me.Btn_MoveTop.IsEnabled = True
        'Me.Btn_MoveBottom.IsEnabled = True
    End Sub

    Private Sub Btn_AddChildSLS_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_AddChildSLS.Click
        Try

            Dim StrMess As String, StrSQL As String, Rst As DataTable, IntChk As Integer
            Dim selectedSLS As DataRowView = Me.List_SLS.SelectedItem
            If selectedSLS Is Nothing Then Exit Sub

            If Me.Combo_CopyFrom.SelectedIndex = -1 Then
                Call MsgBox("Please chose the SLS to add", , "Error")
                Me.Combo_CopyFrom.Focus()
                Exit Sub
            End If


            'Only do checks if there are already Links in this SLS.

            StrSQL = "Select SLSLink_ID from I_SL_SLSLink Where SLSLink_SLSID = " & selectedSLS(0)
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)
            If Rst.Rows.Count = 0 Then
                IntChk = 0
            Else
                IntChk = 1
            End If
            Rst.Dispose()

            'If the child SLS is empty then abort
            StrSQL = "Select SLSLink_ID from I_SL_SLSLink Where SLSLink_SLSID = " & Me.Combo_CopyFrom.SelectedValue
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)
            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                Call MsgBox("That SLS is empty, Add action aborted", , "Error")
                Exit Sub
            End If
            Rst.Dispose()

            StrMess = "SLS number " & Me.Combo_CopyFrom.SelectedValue & " (" & Me.Combo_CopyFrom.Text & ")"
            StrMess = StrMess & " added to SLS number " & selectedSLS(0) & " (" & selectedSLS(1) & ")"

            'If IntChk = 1 Then
            '    StrSQL = Gblfunc_Get_SLSSQL(Me.List_SLS, 0, 0, 3)

            '    StrSQL = StrSQL & " WHERE TrueSLSID In (" & Gblfunc_Get_SLSSQL(Me.Combo_CopyFrom, 0, 0, 3) & ")"

            '    Rst = GSS(StrSQL)

            '    If Rst.RecordCount > 0 Then
            '        Rst.Close()
            '        Call MsgBox("One or more of the Stock Letter Sets in the SLS you are trying to add is already in the current SLS. Possibly at a different level. To avoid the danger of creating a circular reference the add SLS action has been cancelled. Please speak to IT for more information", , "Error")
            '        Exit Sub
            '    End If
            'End If 

            'Call Gblfunc_Add_SLSLink(Me.List_SLS, Me.Combo_CopyFrom, 2, 0, 0, 1000)

            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrMess)

            Call List_SLS_AfterUpdate()

            ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.
           

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminStockLetterSets-Btn_AddChildSLS_Click")
        End Try
    End Sub

    Private Sub Btn_Delete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Delete.Click
        Try
            Dim selectedSLS As DataRowView
            Dim selectedLetterPack As DataRow
            selectedSLS = Me.List_SLS.SelectedItem
            selectedLetterPack = Me.List_LetterPacks.SelectedItem
            If selectedSLS Is Nothing Then Exit Sub

            If selectedLetterPack Is Nothing Then
                Call MsgBox("Please select Letterpack to remove", , "Error")
                Exit Sub
            End If

            Call Gblfunc_Remove_SLSLink(selectedLetterPack(0))
            SQL_LetterPackList()
            'Me.List_LetterPacks.Requery()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminStockLetterSets-Btn_Delete_Click")
        End Try
    End Sub
    Public Sub Gblfunc_Remove_SLSLink(ByVal SLSLinkID As Long)
        Try
            Dim StrSQL As String, Rst As DataTable, StrMess As String
            Dim IntSLS As Integer, IntOrder As Integer, LngLP As Long, IntCSLS As Integer



            StrSQL = "Select SLSLink_SLSID, SLSLink_Order, SLSLink_LetterpackID, SLSLink_ChildSLSID from I_SL_SLSLink where SLSLink_ID = " & SLSLinkID
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                Exit Sub
            End If

            IntSLS = Rst.Rows(0)("SLSLink_SLSID")
            IntOrder = Rst.Rows(0)("SLSLink_Order")
            IntCSLS = Rst.Rows(0)("SLSLink_ChildSLSID")
            LngLP = Rst.Rows(0)("SLSLink_LetterpackID")

            Rst.Dispose()

            'Build sys event message
            If IntCSLS > 0 Then
                'Removing an SLS link
                StrSQL = "Select SLS_Name from I_SL_StockLetterSets where SLS_ID = " & IntCSLS
                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

                If Rst.Rows.Count = 0 Then
                    Rst.Dispose()
                    StrMess = "Unknown SLS number " & IntCSLS & vbCrLf
                Else
                    StrMess = "SLS number " & IntCSLS & " (" & Rst.Rows(0)("SLS_Name") & ")" & vbCrLf
                    Rst.Dispose()
                End If
            Else
                'Removing a Letterpack link
                StrSQL = "Select Letterpack_Name from D_Letterpacks where Letterpack_ID = " & LngLP
                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

                If Rst.Rows.Count = 0 Then
                    Rst.Dispose()
                    StrMess = "Unknown Letterpack number " & LngLP & vbCrLf
                Else
                    StrMess = "Letterpack number " & LngLP & " (" & Rst.Rows(0)("Letterpack_Name") & ")" & vbCrLf
                    Rst.Dispose()
                End If
            End If

            StrSQL = "Select SLS_Name from I_SL_Stocklettersets where SLS_ID = " & IntSLS
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                StrMess = StrMess & "removed from unknown SLS number " & IntSLS
            Else
                StrMess = StrMess & "removed from SLS number " & IntSLS & " (" & Rst.Rows(0)("SLS_Name") & ")"
                Rst.Dispose()
            End If

            'Message ready, delete record

            StrSQL = "Delete From I_SL_SLSLink where SLSLink_ID = " & SLSLinkID
            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            'Update remaining links order to keep consistent
            StrSQL = "Update I_SL_SLSLink Set SLSLink_Order = SLSLink_Order -1 "
            StrSQL = StrSQL & " Where SLSLink_SLSID = " & IntSLS
            StrSQL = StrSQL & " And SLSLink_Order > " & IntOrder

            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            'Save admin message and exit
            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrMess)

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminStockLetterSets-Gblfunc_Remove_SLSLink")
        End Try
    End Sub

    Private Sub Btn_EMailTemplates_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_EMailTemplates.Click
        Try
            Dim selectedSLS As DataRowView = Me.List_SLS.SelectedItem
            If selectedSLS Is Nothing Then Exit Sub
            Dim formEmailTemplates As New frmAdminSLSEMailTemplates
            formEmailTemplates.List_SLS = selectedSLS(0)
            formEmailTemplates.ShowDialog()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminStockLetterSets-Btn_EMailTemplates_Click")
        End Try
    End Sub

    Private Sub Btn_EditLink_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_EditLink.Click
        Try
            Dim StrSQL As String, Rst As DataTable
            Dim selectedLetterPack As DataRow = Me.List_LetterPacks.SelectedItem
            Dim selectedSLS As DataRowView = Me.List_SLS.SelectedItem
            If selectedLetterPack Is Nothing Then Exit Sub

            'stDocName = "FMA_EditSLSLink"
            'DoCmd.OpenForm(stDocName)
            Dim Form_FMA_EditSLSLink As New frmAdminEditSLSLink

            Form_FMA_EditSLSLink.SLS_LPID = selectedLetterPack(1)
            Form_FMA_EditSLSLink.SLS_SLSID = selectedSLS(0)
            Form_FMA_EditSLSLink.SLS_List_SLS = selectedSLS(1)
            Form_FMA_EditSLSLink.SLS_SLSLinkID = selectedLetterPack(0)
            Form_FMA_EditSLSLink.SLS_LetterPackText = selectedLetterPack(2)

            StrSQL = "SELECT SLSLink_StageStart, SLSLink_StageEnd, SLSLink_CustomerType"
            StrSQL = StrSQL & " FROM I_SL_SLSLink"
            StrSQL = StrSQL & " WHERE (SLSLink_ID = " & selectedLetterPack(0) & ")"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                Call MsgBox("SLS Link not found!", , "Error")
                Exit Sub
            End If
            StrSQL = "select CaseStage_ID,CaseStage_Number,CaseStage_Title from L_casestages"
            Dim caseStagesList As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
            With Form_FMA_EditSLSLink.Combo_End
                .ItemsSource = CType(caseStagesList, IListSource).GetList
                .SelectedValuePath = "CaseStage_Number"
                .DisplayMemberPath = "CaseStage_Title"
            End With
            With Form_FMA_EditSLSLink.Combo_Start
                .ItemsSource = CType(caseStagesList, IListSource).GetList
                .SelectedValuePath = "CaseStage_Number"
                .DisplayMemberPath = "CaseStage_Title"
            End With
            Select Case Rst.Rows(0)("SLSLink_StageStart")

                Case 0
                    Form_FMA_EditSLSLink.Frm_Start = 1

                Case 100
                    Form_FMA_EditSLSLink.Frm_Start = 2

                Case Else
                    Form_FMA_EditSLSLink.Frm_Start = 3
                    Form_FMA_EditSLSLink.Combo_Start.SelectedValue = Rst.Rows(0)("SLSLink_StageStart")
                    'Form_FMA_EditSLSLink.Combo_Start.Enabled = True

            End Select

            Select Case Rst.Rows(0)("SLSLink_StageEnd")

                Case 99
                    Form_FMA_EditSLSLink.Frm_End = 1

                Case 999
                    Form_FMA_EditSLSLink.Frm_End = 2

                Case 1999
                    Form_FMA_EditSLSLink.Frm_End = 3

                Case Else
                    Form_FMA_EditSLSLink.Frm_End = 4
                    Form_FMA_EditSLSLink.Combo_End.SelectedValue = Rst.Rows(0)("SLSLink_StageEnd")

            End Select

            Form_FMA_EditSLSLink.Frm_Client = Rst.Rows(0)("SLSLink_CustomerType")
            Form_FMA_EditSLSLink.ShowDialog()


        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminStockLetterSets-Btn_EditLink_Click")
        End Try
    End Sub
End Class
